package com.rdtravel.converters;

import com.rdtravel.entities.Booking;
import com.rdtravel.services.BookingsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;

public class StringToBookingConverter implements Converter<String, Booking> {
	private final BookingsService bookingsService;

	@Autowired
	public StringToBookingConverter(BookingsService bookingsService) {
		this.bookingsService = bookingsService;
	}

	@Override
	public Booking convert(String id) {
		if (id == null || id.isEmpty()) {
			return null;
		}
		try {
			return bookingsService.findById(Long.parseLong(id));
		} catch (NumberFormatException ex) {
			return null;
		}
	}

}
